package grailsforum

class Post {

    String text
    int position
    Date date;

    static constraints = {
        text(nullable: false, blank: false, minSize: 2)
        position(nullable: false)
        date(nullable: false)
    }

    static belongsTo = [thread: Thread, user: User]
    static hasMany = [votes: Vote]

    static mapping = {

        votes cascade: 'all-delete-orphan'
    }

    public boolean isVotedByUser(User user){

        boolean found = false;

        votes.each {

            if(it.user.id == user.id){
                found = true
                return
            }
        }

        return found
    }

    public Vote getVoteByUser(User user){

        Vote vote = null;

        votes.each {

            if(it.user.id == user.id){
                vote = it;
                return
            }
        }

        return vote;
    }
}
